﻿<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<%@ Assembly Name="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="Microsoft.SharePoint.WebControls" %>
<%@ Import Namespace="System.Data" %>
<%@ Register TagPrefix="wssuc" TagName="ToolBar" Src="~/_controltemplates/ToolBar.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="ToolBarButton" Src="~/_controltemplates/ToolBarButton.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="ButtonSection" Src="/_controltemplates/ButtonSection.ascx" %>

<script runat="server">

    protected SPWeb _spWeb = null;
    protected SPList _spList = null;
    //Keep this name in sync with SecureTaskItemReceiver
    private static readonly string configPropertyName = "#@:SecureTask:@#";

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Guid _spListId = Guid.Empty;

        try
        {
            _spWeb = SPControl.GetContextWeb(Context);
            _spListId = new Guid(Server.UrlDecode(Request.QueryString["ListId"].ToString()));
            _spList = _spWeb.Lists[_spListId];
        }
        catch (Exception)
        {
            throw new Exception(String.Format("Unable to locate list {0} in SPWeb {1}.", _spListId.ToString(), _spWeb.Title));
        }
    }
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // No need to validate the list type. Only a property can be added even if user manually reaches for non-task list types.
            
            SPFolder rootFolder = _spList.RootFolder;
            if (rootFolder.Properties.Contains(configPropertyName))
            {
                //load selected item from property bag
                radioButtonListConfig.SelectedIndex = Convert.ToInt32(rootFolder.Properties[configPropertyName]);
            }
            else
            {
                // render item at index 0 as selected and save in property bag
                radioButtonListConfig.SelectedIndex = 0;
            }
        }
    }

    public void SubmitButton_Click(object sender, System.EventArgs e)
    {
        Microsoft.SharePoint.Utilities.SPUtility.ValidateFormDigest();
        SPFolder rootFolder = _spList.RootFolder;
        rootFolder.Properties[configPropertyName] = radioButtonListConfig.SelectedValue;
        rootFolder.Update();
        RedirectToSource();
    }

    protected void RedirectToSource()
    {
        string sourceUrl = Request.QueryString["Source"].ToString();
        Response.Redirect(sourceUrl);
    }

</script>

<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea">
    Secure Task on list : <%=_spList.Title %>
</asp:Content>
<asp:Content ID="Content3" runat="server" ContentPlaceHolderID="PlaceHolderPageDescription">
    Use this page to configure SecureTask feature.
</asp:Content>
<asp:Content ID="Content4" runat="server" ContentPlaceHolderID="PlaceHolderMain">
    <br />
    System account, Site admin, Full Control permission level users, or Assigned To users will be able to edit the task anyway.<br/>
    Use the below section to manage the scope of actions on task by any other class of users.<br/><br/>
    <table border="0" cellspacing="0" cellpadding="0" width="100%">
        <tr>            
            <td>
                <asp:RadioButtonList ID="radioButtonListConfig" runat=server>
                    <%--IF the property is not set for list, then event receiver will secure only 'marking status as completed',
                    Hence This choice should always be at index zero, as no property set means render index 0 as selected.--%>
                    <asp:ListItem Text=" Secure ONLY 1 action 'marking task status as Completed'" Value="0"></asp:ListItem>
                    <asp:ListItem Text=" Secure ALL actions on task" Value="1"></asp:ListItem>
                </asp:RadioButtonList>
                <wssuc:ButtonSection runat="server" ShowStandardCancelButton="true">
                    <template_buttons>
                        <asp:PlaceHolder runat="server">
                            <asp:Button id="btnSubmit" UseSubmitBehavior="false" runat="server" class="ms-ButtonHeightWidth" 
                                    Text="Submit" OnClick="SubmitButton_Click" />
                        </asp:PlaceHolder>
                    </template_buttons>
                </wssuc:ButtonSection>
            </td>
        </tr>
    </table>
</asp:Content>
